package com.ptadmin.report.mws.sample;

import java.util.Arrays;
import java.util.GregorianCalendar;

import javax.xml.datatype.DatatypeConfigurationException;
import javax.xml.datatype.DatatypeFactory;
import javax.xml.datatype.XMLGregorianCalendar;

import com.amazonaws.mws.MarketplaceWebService;
import com.amazonaws.mws.MarketplaceWebServiceClient;
import com.amazonaws.mws.MarketplaceWebServiceConfig;
import com.amazonaws.mws.MarketplaceWebServiceException;
import com.amazonaws.mws.model.IdList;
import com.amazonaws.mws.model.ReportRequestInfo;
import com.amazonaws.mws.model.RequestReportRequest;
import com.amazonaws.mws.model.RequestReportResponse;
import com.amazonaws.mws.model.RequestReportResult;
import com.amazonaws.mws.model.ResponseMetadata;

public class RequestReport {

	public static void main(String[] args) {
		/************************************************************************
         * Access Key ID and Secret Access Key ID, obtained from:
         * http://aws.amazon.com
         ***********************************************************************/
        final String accessKeyId = "AKIAI2JEZAYVZZZ3GHGA";
        final String secretAccessKey = "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";

        final String appName = "ptadmin";
        final String appVersion = "1.0.0";

        MarketplaceWebServiceConfig config = new MarketplaceWebServiceConfig();
        
        config.setServiceURL("https://mws.amazonservices.com/");
        
        MarketplaceWebService service = new MarketplaceWebServiceClient(
                accessKeyId, secretAccessKey, appName, appVersion, config);
        
        final String merchantId = "A2N95VZZXMVRLW";
        final String sellerDevAuthToken = "amzn.mws.52b2d181-1d65-65a8-04f7-e12ce26cfad5";
        
        final IdList marketplaces = new IdList(Arrays.asList(
        		"ATVPDKIKX0DER"));
        
        RequestReportRequest request = new RequestReportRequest()
		        .withMerchant(merchantId)
		        .withMarketplaceIdList(marketplaces)
		        .withReportType("_GET_FLAT_FILE_OPEN_LISTINGS_DATA_")
		        .withReportOptions("ShowSalesChannel=true");
        
        DatatypeFactory df = null;
        
        try {
			df = DatatypeFactory.newInstance();
		} catch (DatatypeConfigurationException e) {
			e.printStackTrace();
			throw new RuntimeException(e);
		}
        
        XMLGregorianCalendar startDate = df
				.newXMLGregorianCalendar(new GregorianCalendar(2011, 1, 1));
		request.setStartDate(startDate);
		
		
		XMLGregorianCalendar endDate = df
				.newXMLGregorianCalendar(new GregorianCalendar(2011, 11,23));
		request.setEndDate(endDate);
		
		// @TODO: set additional request parameters here
		
		invokeRequestReport(service, request);
	}

	/**
     * Request Report  request sample
     * requests the generation of a report
     *   
     * @param service instance of MarketplaceWebService service
     * @param request Action to invoke
     */
    public static void invokeRequestReport(MarketplaceWebService service, RequestReportRequest request) {
    	try {
    		RequestReportResponse response = service.requestReport(request);
    		
    		System.out.println ("RequestReport Action Response");
            System.out.println ("=============================================================================");
            System.out.println ();
            
            System.out.print("    RequestReportResponse");
            System.out.println();
            
            if (response.isSetRequestReportResult()) {
            	System.out.print("        RequestReportResult");
                System.out.println();
                
                RequestReportResult  requestReportResult = response.getRequestReportResult();
                if (requestReportResult.isSetReportRequestInfo()) {
                    System.out.print("            ReportRequestInfo");
                    System.out.println();
                    ReportRequestInfo  reportRequestInfo = requestReportResult.getReportRequestInfo();
                    if (reportRequestInfo.isSetReportRequestId()) {
                        System.out.print("                ReportRequestId");
                        System.out.println();
                        System.out.print("                    " + reportRequestInfo.getReportRequestId());
                        System.out.println();
                    }
                    if (reportRequestInfo.isSetReportType()) {
                        System.out.print("                ReportType");
                        System.out.println();
                        System.out.print("                    " + reportRequestInfo.getReportType());
                        System.out.println();
                    }
                    if (reportRequestInfo.isSetStartDate()) {
                        System.out.print("                StartDate");
                        System.out.println();
                        System.out.print("                    " + reportRequestInfo.getStartDate());
                        System.out.println();
                    }
                    if (reportRequestInfo.isSetEndDate()) {
                        System.out.print("                EndDate");
                        System.out.println();
                        System.out.print("                    " + reportRequestInfo.getEndDate());
                        System.out.println();
                    }
                    if (reportRequestInfo.isSetSubmittedDate()) {
                        System.out.print("                SubmittedDate");
                        System.out.println();
                        System.out.print("                    " + reportRequestInfo.getSubmittedDate());
                        System.out.println();
                    }
                    if (reportRequestInfo.isSetReportProcessingStatus()) {
                        System.out.print("                ReportProcessingStatus");
                        System.out.println();
                        System.out.print("                    " + reportRequestInfo.getReportProcessingStatus());
                        System.out.println();
                    }
                }
            }
            if (response.isSetResponseMetadata()) {
                System.out.print("        ResponseMetadata");
                System.out.println();
                ResponseMetadata  responseMetadata = response.getResponseMetadata();
                if (responseMetadata.isSetRequestId()) {
                    System.out.print("            RequestId");
                    System.out.println();
                    System.out.print("                " + responseMetadata.getRequestId());
                    System.out.println();
                }
            }
            
            System.out.println();
            System.out.println(response.getResponseHeaderMetadata());
            System.out.println();
            
    	} catch (MarketplaceWebServiceException ex) {
    		System.out.println("Caught Exception: " + ex.getMessage());
            System.out.println("Response Status Code: " + ex.getStatusCode());
            System.out.println("Error Code: " + ex.getErrorCode());
            System.out.println("Error Type: " + ex.getErrorType());
            System.out.println("Request ID: " + ex.getRequestId());
            System.out.print("XML: " + ex.getXML());
            System.out.println("ResponseHeaderMetadata: " + ex.getResponseHeaderMetadata());
    	}
    }
}
